package com.mh.www.mh_frame.util;

import android.app.Activity;

import java.util.Iterator;
import java.util.Stack;

/**
 * @author think
 * @version [版本号, 2018/3/13]
 * @describe [描述] activity 栈管理 用于退出单个activity 退出全部activity
 * @see [相关类/方法]
 * @since [V1]
 */

public class ActivityStackManager {
    private Stack<Activity> activityStack;

    public static ActivityStackManager getInstance(){
      return SingletonHolder.instance;
    }

    private static class SingletonHolder {
        private static final ActivityStackManager instance = new ActivityStackManager();
    }

    private ActivityStackManager(){

    }

    /**
     * 把一个activtiy压入栈中
     * @param activity
     */
    public void pushOneActivity(Activity activity){
        if (activity==null){
            return;
        }
        if (activityStack == null){
            activityStack = new Stack<>();
        }
        activityStack.push(activity);
    }


    /**
     * 弹出栈顶的activity
     * @param activity
     */
    public void popOneActivity(Activity activity){
        if (activity==null || CollectionUtil.isEmpty(activityStack)){
            return;
        }
        activityStack.pop();

    }

    /**
     * 获取栈顶的Activity
     * @return
     */
    public Activity currentActivity(){
        Activity activity = null;
        if (!CollectionUtil.isEmpty(activityStack)){
            activity = activityStack.peek();
        }
        return activity;
    }

    /**
     * 获取栈底(第一个压住栈)的activity，先进后出原则
     *
     * @return
     */
    public Activity firstActivity() {
        Activity activity = null;
        if (!CollectionUtil.isEmpty(activityStack)) {
            activity = activityStack.firstElement();
        }
        return activity;
    }

    /**
     * 获取应用 activity的数量
     *
     * @return
     */
    public int getStackSize() {
        if (activityStack != null) {
            return activityStack.size();
        }
        return 0;
    }

    /**
     * 退出activity
     * @param activity
     */
    public void exitActivity(Activity activity) {
        activity.finish();
        activityStack.remove(activity);
    }

    /**
     * 退出类型为cls的Activity
     * @param cls
     */
    public void exitActivity(Class cls) {
        if (CollectionUtil.isEmpty(activityStack)) {
            return;
        }

        Iterator<Activity> iterator = activityStack.iterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            if (activity == null) {
                continue;
            }
            if (activity.getClass().equals(cls)) {
                exitActivity(activity);
            }
        }
    }

    /**
     * 退出类型为cls且第一个进栈的Activity
     * @param cls
     */
    public void exitActivityByFirstIn(Class cls) {
        if (CollectionUtil.isEmpty(activityStack)) {
            return;
        }

        Iterator<Activity> iterator = activityStack.iterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            if (activity == null) {
                continue;
            }
            if (activity.getClass().equals(cls)) {
                exitActivity(activity);
                break;
            }
        }
    }

    /**
     * 退出类型为cls且一个进栈的Activity
     * @param cls
     */
    public void exitActivityByLastIn(Class cls) {
        if (CollectionUtil.isEmpty(activityStack)) {
            return;
        }

        int size = activityStack.size();
        for (int i = size - 1; i >= 0; i--) {
            Activity activity = activityStack.get(i);
            if (activity == null) {
                continue;
            }
            if (activity.getClass().equals(cls)) {
                activity.finish();
                activityStack.removeElementAt(activityStack.lastIndexOf(activity));
                break;
            }
        }
    }

    /**
     * 退出除cls类型以外的所有Activity
     *
     * @param cls 页面activity的类型
     */
    public void exitAllActivityExceptCurrent(Class cls) {
        Activity activity;
        while (true) {
            activity = firstActivity();
            if (activity == null) {
                break;
            }
            if (activity.getClass().equals(cls)) {
                if (getStackSize() <= 1) {
                    break;
                } else continue;
            }
            exitActivity(activity);
        }
    }

    /**
     * 退出应用程序
     */
    public void exitApplication() {
        while (true) {
            Activity activity = firstActivity();
            if (activity == null) {
                break;
            }
            exitActivity(activity);
        }
    }



}
